﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Images;
using System.Drawing;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control to be used by <see cref="T:TextBox"/> to display the tooltip to access clipboard.
    /// </summary>
    public class TextBoxToolTip : ToolTipBase
    {
        const int buttonSpace = 12;
        const int buttonSize = 60;

        private TextBox textBox;

        private List<Control> buttons = new List<Control>(3);

        public TextBoxToolTip()
            : base()
        {
            buttons = new List<Control>();
            ImageButton b1 = new ImageButton { Image = SysImages.Cut, Color = Color.White };
            ImageButton b2 = new ImageButton { Image = SysImages.Copy, Color = Color.White };
            ImageButton b3 = new ImageButton { Image = SysImages.Paste, Color = Color.White };

            b1.Click += new EventHandler(OnCut);
            b2.Click += new EventHandler(OnCopy);
            b3.Click += new EventHandler(OnPaste);
            b1.PressedChanged += new EventHandler(OnPressedChanged);
            b2.PressedChanged += new EventHandler(OnPressedChanged);
            b3.PressedChanged += new EventHandler(OnPressedChanged);

            buttons.Add(b1);
            buttons.Add(b2);
            buttons.Add(b3);

            Size = new Size(buttonSize * 3 + buttonSpace * 2 + 32, buttonSize + 30);
            Alpha = 220;

        }

        void OnPressedChanged(object sender, EventArgs e)
        {
            ImageButton b = sender as ImageButton;
            b.Color = b.IsPressed ? Color.FromArgb(20, 172, 255) : Color.White;
        }

        protected internal override IList<Control> SubControls { get { return buttons; } }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Appearance))
            {
                int x = 0;
                int w = buttonSize, h = buttonSize;
                int y = Position == ToolTipPosition.Above ? 8 : 8 + 16;
                foreach (ImageButton b in buttons)
                {
                    b.Bounds = new Rectangle(16 + x, y, w, h);
                    x += w + buttonSpace;
                }
            }
            base.OnLayout();
        }

        public override void Show(Control anchor)
        {
            textBox = anchor as TextBox;
            base.Show(anchor);
        }

        void OnPaste(object sender, EventArgs e)
        {
            textBox.Paste();
        }

        void OnCopy(object sender, EventArgs e)
        {
            textBox.Copy();
        }

        void OnCut(object sender, EventArgs e)
        {
            textBox.Cut();
        }



    }
}
